//
//  CYViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/6/27.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYViewController.h"
#import "CYNavigationController.h"
#import "CYBettingViewController.h"
#import "CYPopBoxView.h"
@class CYBuyViewController;
@interface CYViewController (){
    NSURLSessionTask *_messageTask;
}

@end

@implementation CYViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // Do any additional setup after loading the view.
}

- (UISwipeGestureRecognizer *)right_swipe{
    if (!_right_swipe) {
        _right_swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
        [_right_swipe setDirection:(UISwipeGestureRecognizerDirectionRight)];
    }
    return _right_swipe;
}

- (UISwipeGestureRecognizer *)left_swipe{
    if (!_left_swipe) {
        _left_swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipe:)];
        [_left_swipe setDirection:(UISwipeGestureRecognizerDirectionLeft)];
    }
    return _left_swipe;
}

- (void)addSwipe{
    [self.view addGestureRecognizer:self.right_swipe];
    [self.view addGestureRecognizer:self.left_swipe];
}

- (void)handleSwipe:(UISwipeGestureRecognizer *)sender{
    
}

//设置导航栏
- (void)setupNavigationBar{
    
    self.navigationController.navigationBar.hidden = NO;//显示导航栏
    self.navigationController.navigationBar.subviews[0].subviews[0].alpha = 1;//设置navigationbar透明度
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self setupNavigationBarTintColor:THEME_COLOR];
    
}

//设置导航栏颜色
- (void)setupNavigationBarTintColor:(UIColor *)color{
    if ([color isEqual:THEME_COLOR]) {//如果状态栏背景是->主题红色
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor whiteColor]}]; //导航栏标题 字体颜色->白色
        [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];  // 设置导航栏左右按钮 字体颜色->白色
    }
    else{
        [self.navigationController.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]}]; //导航栏标题 字体颜色->黑色
        [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];  // 设置导航栏左右按钮 字体颜色->黑色
    }
    [self.navigationController.navigationBar setBarTintColor:color]; //设置导航栏背景颜色
    
    if ([NSStringFromClass([self class]) isEqualToString:@"CYBuyViewController"]){ //首页特殊，状态栏永远为红色
        [self setupStatusBarBackgroundColor:THEME_COLOR];
        [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];  // 设置导航栏左右按钮 字体颜色->白色
    }
    else
        [self setupStatusBarBackgroundColor:color];//设置状态栏背景颜色
}

//设置状态栏颜色
- (void)setupStatusBarBackgroundColor:(UIColor *)color {
    [UIApplication sharedApplication].statusBarStyle = ([color isEqual:THEME_COLOR] || [color isEqual:[UIColor clearColor]])?UIStatusBarStyleLightContent:UIStatusBarStyleDefault;//状态栏字体颜色
    UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
    if ([statusBar respondsToSelector:@selector(setBackgroundColor:)])
        statusBar.backgroundColor = color;//
}



//返回上层viewcontroller
- (void)back{
    [self taskCancel];
    
    [self.navigationController popViewControllerAnimated:YES];
}

//返回上层viewcontroller
- (void)popPreviousViewController{
    [self.navigationController popViewControllerAnimated:YES];
}

//返回上层viewcontroller,并通过kvo给上层viewcontroller
- (void)backWithValues:(NSArray *)values keys:(NSArray *)keys{
    NSUInteger count = self.navigationController.viewControllers.count;
    if ([self.navigationController isKindOfClass:[CYNavigationController class]] ) {
        if ([values count] == [keys count]) {
            for (int i = 0; i < keys.count; i ++) {
                [  self.navigationController.viewControllers[count - 2] setValue:values[i] forKey:keys[i]];
            }
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
}

//弹框消息
- (void)showMessage{
    if ([CYUser shareUser].username && [CYUser shareUser].token )
        _messageTask = [CYHttps POST:API_message parameters:@{@"username":[CYUser shareUser].username,@"token":[CYUser shareUser].token} success:^(id data) {
            //        CYBoxModel *model = [CYBoxModel mj_objectWithKeyValues:data];
            //        if (model.contents)
            //        [CYPopBoxView initWithModel:model];
            NSDictionary *dict = @{@"contents":@"我是个小小的蒲公英 出发要到远方旅行\n我是个小小的蒲公英 那里才是我停留的地方\n风一直吹个不停 我也随着它四处飘散\n从黑暗直到天明 我来打扰你的心情\n我是个小小的蒲公英 出发要到远方旅行\n我是个小小的蒲公英 天空才是我熟悉的地方\n飘过哭泣的小孩 快乐天真都洋溢\n飘过残酷的战场 恐惧的眼神都远离\n我是个小小的蒲公英 出发要到远方旅行\n我是个勇敢的蒲公英 我要到一个有爱的地方\n飘过伤心的恋人 破碎的心得到安宁\n飘过受伤的大地 涂上绿色的美丽\n我是个小小的蒲公英 啦...........\n我是个勇敢的蒲公英 啦...........\n我是个快乐的蒲公英 啦...........[1]"};
            [CYPopBoxView initWithModel:[CYBoxModel mj_objectWithKeyValues:dict]];
            
        } failure:nil];
    
}
- (void)taskCancel{
    [self.task cancel];
    self.task = nil;
    [self.spareTask cancel];
    self.spareTask = nil;
    [_messageTask cancel];
    _messageTask = nil;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setupNavigationBar];
    [self setupNavigationBarTintColor:THEME_COLOR];
    [self showMessage];
    
}

- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    
}

- (void)dealloc{
    [self taskCancel];
    DeLog(@" !!  self  -->  dealloc  %@!!",self);
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
